<?php

require_once 'BlogpostsHandler.php';

class BlogpostreadView{
	
	private $id = "&id=";
	private $title = "title";
	private $post = "post";
	private $author = "author";
	private $deletePostID = "deletePostID";
	private $deletePost ="deletePost";
	private $nav = "?Comments";
	private $editNav = "?Edit";
	
	const DELETED = 0;
	
	//Hämtar och visar alla inlägg som admin tillsammans med en raderaknapp.
	public function ViewBlogpostsAsAdmin($blogposts){
	$ret = "";
	foreach ($blogposts as $key => $value) {
		
		$ret .= '<div id="blogreadposts">
			<div></br></br></br>
			<h2 id="viewtitle">'.nl2br($value->getTitle()).'</h2></br>
			<p id="viewpost">'.nl2br($value->getPost()).'</p></br>	
			<p id="viewauthor">Skapad av:  '.$value->getAuthor().'</p>			
			</div>
			
			<div id="commenteditLink">
			<a id="commentLink" href="' . $this->nav . $this->id . $value->getId() .'">Kommentarer</a>
			<a id="editLink" href="' . $this->editNav . $this->id . $value->getId() .'">Redigera</a>
				 	
		 	<form action="" method="post">
			  <input name="'.$this->deletePostID.'" type="hidden" value="'.$value->getId().'" />
			  <input name="'.$this->deletePost.'" type="submit" id="deletebutton" value="X" />
			  </form>
			</div>
			
		</div>' ;		
		}
		return $ret;		
	}
	//Funktion för att visa alla skrivna inlägg.
	public function ViewBlogposts($blogposts){
	$ret = "";
	foreach ($blogposts as $key => $value) {
		
		$ret .= '<div id="blogreadposts">
			<div></br></br></br>
			<h2 id="viewtitle">'.nl2br($value->getTitle()).'</h2></br>
			<p id="viewpost">'.nl2br($value->getPost()).'</p></br>
			<p id="viewauthor">Skapad av:  '.$value->getAuthor().'</p>			
			</div>
					
			<a id="commentLink" href="' . $this->nav . $this->id . $value->getId() .'">Kommentera</a>
			
		</div>' ;		
		}
		return $ret;	
	}
	
	
	public function DeletePost($deleteID)
	 {
			 return "<form action='' method='post'>
			 <input name='$this->deletePostID' type='hidden' value='$deleteID' />
			 <input name='$this->deletePost' type='submit' value='Radera' />
			 </form>";		
	 }
	 
	 //Tar bort en specifik inlägg.
	public function DeletePostid(){
		 if(isset ($_POST[$this->deletePostID])){
			 return $_POST[$this->deletePostID];
		 }
	 }
	//Förösker ta bort en inlägg.
	 public function TriedToDeletePost(){
		 if(isset($_POST[$this->deletePostID])){
			 return TRUE;
		}
		 return FALSE;
	 }
	 
	 public static function Message($n){
				$message = null;				
				switch($n){					
					 case self::DELETED:
						 $message .= "<p id='fieldtext'>Inlägget raderat</p>";
						 $message .= "<p id='fieldtext'>Inlägget borttaget.</p>";
						 break;
				}			
			
			return "<p class='message'> $message</p>";			
		}			
}
